﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
namespace BAITAP2_LTCSDL
{
    public partial class ThemHS : Form
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("HOSOHS");
        private SqlDataAdapter da = new SqlDataAdapter();

        public ThemHS()
        {
            InitializeComponent();
        }

        private void ThemHS_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }
        public void connect()
        {
            string conn = "Data Source=(local);Integrated Security = true; Database=QUANLIHS_CAP3";
            try
            {
                con = new SqlConnection(conn);
                con.Open();
                //con = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("khong ket noi duoc database");
            }
        }
        public void disconnect()
        {
            con.Close();
            con.Dispose();
        }
        private void getdata()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * from HOSOHS";
            da.SelectCommand = cmd;
            da.Fill(dt);

        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            Form1 frmMain = new Form1();
            frmMain.Show();

        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            DataRow row = dt.NewRow();
            //row["MAHS"]=txt
            row["HOTEN"] = txt_hoten.Text;
            row["GIOITINH"] = dateTimePicker1.Value;
            row["DIACHI"] = txt_diachi.Text;
            row["EMAIL"] = txt_email.Text;
            dt.Rows.Add(row);
            SqlCommand cmdThem = new SqlCommand();
            cmdThem.Connection = con;
            cmdThem.CommandType = CommandType.Text;
            cmdThem.CommandText = @"Insert HOSOHS (MAHS,HOTEN,GIOITINH,NGAYSINH,DIACHI,EMAIL)
            values (@MAHS,@HOTEN,@GIOITINH,@NGAYSINH,@DIACHI,@EMAIL)";
            cmdThem.Parameters.Add("@MAHS", SqlDbType.NChar, 10).Value = txt_mahs.Text;
            cmdThem.Parameters.Add("@HOTEN", SqlDbType.NVarChar, 50).Value = txt_hoten.Text;
            cmdThem.Parameters.Add("@GIOITINH", SqlDbType.NChar, 10).Value = comboBox1.Text;
            cmdThem.Parameters.Add("@NGAYSINH", SqlDbType.Date).Value = dateTimePicker1.Text;
            cmdThem.Parameters.Add("@DIACHI", SqlDbType.NVarChar, 50).Value = txt_diachi.Text;
            cmdThem.Parameters.Add("@EMAIL", SqlDbType.NVarChar, 50).Value = txt_email.Text;
            da.InsertCommand = cmdThem;
            da.Update(dt);
            MessageBox.Show("Update thanh cong!");
            Close();
            Dispose();
            disconnect();
            Form1 frmMain = new Form1();
            frmMain.Show();

        }
    }
}
